package com.java.util;

import com.java.util.pojo.AdComponentDetailDto;

import java.util.*;
import java.util.stream.Collectors;

public class ListToFilterMap {
    public static final String SINGLE_VARIANT = "$singleVariant";
    public static final String PREFERRED_IMAGE_VARIANT = "$preferred";
    private static final String SYSTEM_ASSET_USER_NAME = "System";


    public static void main(String[] args) {
        List<AdComponentDetailDto> adAssetVoList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            AdComponentDetailDto detailDto = AdComponentDetailDto.builder()
                    .createBy(i + "").build();

            adAssetVoList.add(detailDto);
        }

        Map<String, List<AdComponentDetailDto>> image = getAdAssetVos(adAssetVoList, "IMAGE");

        System.out.println(">>>>>>>>>> ");
    }


    /**
     * 将List集合转成Map<String, List<AdComponentDetailDto>>后，
     * 对每个key的value值，进行处理，特定的key处理返回特定的List<AdComponentDetailDto> value值。
     *
     * @param adAssetVoList
     * @param contentAdComponentType
     * @return
     */
    private static Map<String, List<AdComponentDetailDto>> getAdAssetVos(List<AdComponentDetailDto> adAssetVoList, String contentAdComponentType) {

        Map<String, List<AdComponentDetailDto>> resultMap = adAssetVoList.stream()
                .collect(Collectors.groupingBy(
                        e -> {
                            if ("IMAGE".equals(contentAdComponentType) && SYSTEM_ASSET_USER_NAME.equalsIgnoreCase(e.getCreateBy())) {
                                return PREFERRED_IMAGE_VARIANT;

                            } else {
                                return Objects.equals(e.getSingleVariant(), Boolean.TRUE) ? SINGLE_VARIANT : e.getDisplayName();
                            }
                        }))
                .entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> {
                            if (PREFERRED_IMAGE_VARIANT.equals(entry.getKey())) {
                                return entry.getValue().stream()
                                        .filter(e -> e.getFinalURL() != null && e.getFinalURL().length() != 0)
                                        .collect(Collectors.toList());
                            }

                            return entry.getValue();
                        }
                ));


        for (Map.Entry<String, List<AdComponentDetailDto>> entry : resultMap.entrySet()) {
            List<AdComponentDetailDto> list = entry.getValue();
            if (SINGLE_VARIANT.equals(entry.getKey())) {
                list.sort(Comparator.comparing(AdComponentDetailDto::getDisplayName, Comparator.nullsLast(Comparator.naturalOrder())));
            } else {
                list.sort(Comparator.comparing(AdComponentDetailDto::getVariant, Comparator.nullsLast(Comparator.naturalOrder())));
            }
        }
        return new TreeMap<>(resultMap);
    }
}
